/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.adurn.architecture.dao;

import br.ufrn.adurn.domain.Departamento;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Victor
 */
public class DepartamentoDAO {

    private Connection connection;

    public DepartamentoDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    public void adiciona(Departamento departamento) {
        String sql = "INSERT INTO departamentos (id_centro, nome, email, tel1, tel2) values (?,?,?,?,?)";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setLong(1, departamento.getIdCentro());
            stmt.setString(2, departamento.getNome());
            stmt.setString(3, departamento.getEmail());
            stmt.setString(4, departamento.getTel1());
            stmt.setString(5, departamento.getTel2());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        JOptionPane.showMessageDialog(null, "Centro " + departamento.getNome() + " adicionado com sucesso", "", JOptionPane.INFORMATION_MESSAGE);
    }

    public List<Departamento> getLista() {
        try {
            List<Departamento> departamentos = new ArrayList<Departamento>();
            PreparedStatement stmt = this.connection.prepareStatement("select * from departamentos order by nome asc");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Departamento departamento = new Departamento();
                departamento.setIdDepartamento(rs.getLong("id_departamento"));
                departamento.setIdCentro(rs.getLong("id_centro"));
                departamento.setNome(rs.getString("nome"));
                departamento.setEmail(rs.getString("email"));
                departamento.setTel1(rs.getString("tel1"));
                departamento.setTel2(rs.getString("tel2"));
                departamentos.add(departamento);
            }
            rs.close();
            stmt.close();
            return departamentos;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Long getIdByNome(String nome) {
        Long idDepto = null;
        try {
            PreparedStatement stmt = this.connection.prepareStatement("select id_departamento from departamentos where nome like '" + nome + "'");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                idDepto = rs.getLong("id_departamento");
            }
            rs.close();
            stmt.close();
            return idDepto;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void remove(Departamento departamento) {
        try {
            PreparedStatement stmt = connection.prepareStatement("delete from departamentos where id_departamento=?");
            stmt.setLong(1, departamento.getIdDepartamento());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
